/*
 * @Description: 
 * @Author: houcw
 * @Date: 2024-01-25 14:43:03
 * @LastEditTime: 2024-01-25 15:59:24
 * @LastEditors: houcw
 * @Reference: 
 */
var express = require('express');
const crypto = require('crypto');
var router = express.Router();
let User = require('../model/User');

const key = '0123456789abcdefghijklmn'
const iv = '0123456789abcdef'
const algorithm = 'aes192'
const encoding = 'hex'
const cipher = crypto.createCipheriv(algorithm, key, iv)

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

// 用户注册
router.post('/register', function (req, res, next) {
  const username = req.body.username
  User.findOne({ username: username }, (err, data) => {
    if (err) {
      res.json({
        status: 400,
        msg: "数据读取失败！"
      });
    } else if (data) {
      console.log(data,'查询结果')
      res.json({
        status: 200,
        msg: "该用户名已被注册，请选用其他用户名"
      });
    } else {
      cipher.update(req.body.password)
      cipher.final(encoding)
      User.create({ ...req.body }, (err1, data1) => {
        if (err1) {
          res.json({
            status: 400,
            msg: err.message
          });
        } else {
          res.json({
            status: 200,
            msg: '新增成功'
          })
        }
      })
    }
  })
})

module.exports = router;
